#!/bin/sh

########################################
##### Post Install Script
########################################

####################
##### Variables
####################
##### Determine version of OSX.
foundVersion=(`sw_vers -productVersion`)

##### Check to see, if they are running 10.4.
testResult=`echo $foundVersion | grep '10.4'`



########################################
##### ICU
########################################

####################
##### This will allow us to uninstall LP8 without endangering anyone else using ICU
##### on the local file system.
####################

#####
if [ ! -r /usr/local ]; then
	echo "Creating missing /usr/local folder, and setting permissions"
	mkdir -p /usr/local
	chmod 755 /usr/local
	chown root:wheel /usr/local
fi

#####
if [ ! -r /usr/local/lib ]; then
	echo "Creating missing /usr/local/lib folder, and setting permissions"
	mkdir -p /usr/local/lib
	chmod 755 /usr/local/lib
	chown root:wheel /usr/local/lib
fi

#####
if [ ! -r /usr/local/etc ]; then
        echo "Creating missing /usr/local/etc folder, and setting permissions"
        mkdir -p /usr/local/etc
        chmod 755 /usr/local/etc
        chown root:wheel /usr/local/etc
fi

#####
#if [ ! -r /usr/local/share/icu/3.6 ]; then
#	echo "Creating missing /usr/local/share/icu/3.6 folder and setting permissions."
#	mkdir -p /usr/local/share/icu/3.6
#	chmod 755 /usr/local/share/icu/3.6
#	chown root:wheel /usr/local/share/icu/3.6
#fi


##### Move ICU .dat files
cd /usr/local/share/icu/3.6
mv -f /private/lassolibs8.5/icudt36b.dat /usr/local/share/icu/3.6
mv -f /private/lassolibs8.5/icudt36l.dat /usr/local/share/icu/3.6



########################################
##### LIBPCRE
########################################
##### 

	
##### Before removing PCRE libraries, copy libpcre.0.0.1.dylib
#mv /private/lassolibs8.5/libpcre.0.0.1.dylib /usr/local/lib
#mv /private/lassolibs8.5/libpcre.0.dylib /usr/local/lib
#mv /private/lassolibs8.5/libpcre.dylib /usr/local/lib
	
##### Remove PCRE libraries
#sudo rm /private/lassolibs8.5/libpcre*

if [ ! $testResult ]; then
	##### If it is not 10.4, remove the SpotlightDS.dylib
	sudo rm /Applications/Lasso\ Professional\ 8/LassoModules/SpotlightDS.dylib
fi


########################################
##### Datasource libraries
########################################

####################
##### FreeTDS (SQL Server) libraries
####################
cd /private/lassolibs8.5/

##### freetds.conf
if [ ! -f /usr/local/etc/freetds.conf ]; then
	/usr/bin/install -c -m 644 /private/lassolibs8.5/freetds.conf /usr/local/etc/	
fi 

##### locales.conf
if [ ! -f /usr/local/etc/locales.conf ]; then
	/usr/bin/install -c -m 644 /private/lassolibs8.5/locales.conf /usr/local/etc/
fi

rm -f /private/lassolibs8.5/freetds.conf
rm -f /private/lassolibs8.5/locales.conf


##### libsybdb.5.0.dylib
if [ ! -r /usr/local/lib/libsybdb.5.0.dylib ]; then
	cp /private/lassolibs8.5/libsybdb.5.0.dylib 	/usr/local/lib/
	
	cd /usr/local/lib
	ln -s libsybdb.5.0.dylib libsybdb.5.dylib
	ln -s libsybdb.5.0.dylib libsybdb.dylib
else
	rm -f /private/lassolibs8.5/libsybdb.5.0.dylib
fi

####################
##### iODBC
####################
ln -s /usr/lib/libiodbc.2.dylib /usr/local/lib/libiodbc.dylib.2
ln -s /Library/ODBC /Applications/Lasso\ Professional\ 8/Library/



########################################
##### 3rd Party Modules
########################################

####################
##### ImageMagick Libraries (OP Build)
####################

##########
##### Symlink ImageMagick folder to /
##########
ln -s "/Applications/Lasso Professional 8/LassoImageMagick.6" /

##########
##### Modify .hidden file to hide Symlink in finder
##########
if [ -L /LassoImageMagick.6 ]; then
        
	cd /

	if [ ! -f /.hidden ]; then
		echo "No .hidden file.  Creating .hidden file."
		
		##### Create .hidden file
		echo "LassoImageMagick.6" > /.hidden
	else

		echo "Found .hidden file, checking..."
		if grep -qe "LassoImageMagick.6" /.hidden
		then
			echo "Entry found in .hidden file, aborting"
		else
			echo "No entry found in .hidden file, adding..."
			rm -f /.hidden.tmp
			cp /.hidden /.hidden.tmp
			ed - /.hidden.tmp > /dev/null << 'HIDDENMOD'
$
a
LassoImageMagick.6
.
w
q
HIDDENMOD

			BEFORE=`cat /.hidden | wc -l`
			AFTER=`cat /.hidden.tmp | wc -l`

			if [ $BEFORE -lt $AFTER ]; then
				mv /.hidden.tmp /.hidden
			else
				echo "  Bad result. Something went wrong in the modification of the .hidden file. The lines before are greater than or equal to the resulting new file."
			fi

		fi
	fi

	##### Change permissions back.
	chmod 444 /.hidden

	##### Kill the finder for this to take effect.
	killall Finder &
fi


####################
##### Chart FX
####################
cd /Applications/Lasso\ Professional\ 8/JavaLibraries/

if [ ! -r ChartFX.Internet.config ]; then
	cp /Applications/Lasso\ Professional\ 8/Chart\ FX/Chart\ FX\ Config/ChartFX.Internet.config /Applications/Lasso\ Professional\ 8/JavaLibraries/
fi



########################################
##### Lasso file modifications.
########################################
####################
##### If OSProcess module is installed, update it.
####################
cd /Applications/Lasso\ Professional\ 8/LassoModules

if [ -r OS_Process.dylib ]; then
	rm -f OSProcess.dylib
	cp ../Extensions/OS_Process/OS_Process.dylib .
fi

####################
##### Remove unnecessary files.
####################
cd /usr/local/lib

if [ ! -r libicudata.dylib ]; then
	##### This was in an if statement before; however, it will now run on both upgrades and installs.  This is to ensure that any symlinks to previous libraries are moved properly.
	##### Delete all symlinks and move libraries.
	rm -f libicudata.dylib
	rm -f libicudata.dylib.34
	rm -f libicui18n.dylib
	rm -f libicui18n.dylib.34
	rm -f libicuio.dylib
	rm -f libicuio.dylib.34
	rm -f libicule.dylib
	rm -f libicule.dylib.34
	rm -f libiculx.dylib
	rm -f libiculx.dylib.34
	rm -f libicutu.dylib
	rm -f libicutu.dylib.34
	rm -f libicuuc.dylib
	rm -f libicuuc.dylib.34
	rm -f libsqlite3-3.2.8.0.dylib
	rm -f libsqlite3.dylib
fi

##### Move all remaining files.
mv -f /private/lassolibs8.5/* /usr/local/lib



########################################
#####
########################################

cd ~lasso

##### temp lib folder for crash reporting
chown -R lasso:admin Library
chmod -R 775 Library

if [ -f LassoErrors.txt ]; then
	chown lasso:admin LassoErrors.txt
	chmod 660 LassoErrors.txt
fi

/usr/sbin/lasso8ctl start

if [ "$COMMAND_LINE_INSTALL" = 1 ]; then
	echo "We are in command line mode. No window opening."
else
	if [ ! -f /private/tmp/ls8install ]; then
		open . > /dev/null 2>&1
	fi
fi
